/*
 * table_TypeInfo.c
 *
 *  Created on: 2012/03/29
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"

typedef struct _TYPEDEFINFO
{
	UCHAR		ucKind;				/* 2 */
	UCHAR		ucSigned;			/* 3 */
	UCHAR		ucPrecision;		/* 4 */
	UCHAR		ucIntorFloat;		/* 5 */
	UCHAR		ucTypeQualifier;	/* 6 */
	UCHAR       ucVariableSize;
	UCHAR       ucUndefined;
	U4			ulSize;					/* 8 */
	AST			astOrigin;				/* 9 */
	AST         astNoQual;
	AST			astSymChain;		/* 10 */
	AST			astName;
	AST         astNext;
	RBTREE      rbtree;
} TYPEDEFINFO, *PTYPEDEFINFO;

#define TableSize  (sizeof(TYPEDEFINFO ))
#define TableType  PTYPEDEFINFO
#define stTable stTypeInfoManager.memoryBlocks
#define maxTable stTypeInfoManager.maxRecordNum
#define  maxNumPerBlk  (stTypeInfoManager.astMaxNum_Block)

static TABLEMANAGER stTypeInfoManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};
static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stTypeInfoManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stTypeInfoManager, resetListener,  index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	return Clear_Com( &stTypeInfoManager );
}
static BOOL ResetTable( VOID )
{
	return Reset_Com( &stTypeInfoManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stTypeInfoManager, astReqArg, resetListener );
}
/**************************************************************************
* 関 数 名 : copyTable
* 機    能 : 同じテーブルの行のブロックを移動する
* 引 き 数 ：
* 　　　　　　　astOrg :コピー元の開始行の連番
* 		　astDst：　コピー先の開始行の連番
*         astlenth: ブロック中の行の数
* 戻 り 値 : 成功する場合、1。失敗する場合、0
* 詳細内容 :  メモリ移動
************************************************************************/
static BOOL copyTable( AST  astOrg,  AST  astDst,  AST astLenth )
{
	return Copy_Com( &stTypeInfoManager, resetListener, astOrg, astDst, astLenth );
}

static AST getMaxLineNum( VOID )
{
	return maxTable;
}
//	USHORT_TYPEKIND
/* get method */
static U2  getTypeKind( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucKind ;
}
/* set method */
static U2  setTypeKind( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucKind = value;
}
//	USHORT_SIGNFLG
static U2  getSignFlag( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucSigned ;
}
/* set method */
static U2  setSignFlag( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucSigned = value;
}
//	USHORT_LONGSHORT
static U2  getLongShortFlg( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucPrecision ;
}
/* set method */
static U2  setLongShortFlg( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucPrecision = value;
}
//	USHORT_INTFLOAT
static U2  getIntFloatFlag( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucIntorFloat ;
}
/* set method */
static U2  setIntFloatFlag( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucIntorFloat = value;
}
//	USHORT_TYPEQUAL
static U2  getTypeQual( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucTypeQualifier ;
}
/* set method */
static U2  setTypeQual( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucTypeQualifier = value;
}
static BOOL isVariableSize( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucVariableSize != 0 ;
}
/* set method */
static BOOL  setVariableSize( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucVariableSize = value;
}
static BOOL isUndefined( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ucUndefined ;
}
/* set method */
static BOOL  setUndefined( AST index, BOOL value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ucUndefined = value;
}
//	U4_SIZE
static U4  getSize( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->ulSize ;
}
/* set method */
static U4  setSize( AST index, U4 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->ulSize = value;
}
//	IDX_TYPEDEFINFO
static AST  getElementType( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astOrigin;
}
/* set method */
static AST  setElementType( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astOrigin = value;
}
//	IDX_TYPEDEFINFO
static AST  getNoQualType( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astNoQual;
}
/* set method */
static AST  setNoQualType( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astNoQual = value;
}
//	IDX_DEFCHAIN
static AST  getMemChain( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astSymChain;
}
/* set method */
static AST  setMemChain( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astSymChain = value;
}
//	IDX_SYMDEFINFO
static AST  getTagInfo( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astName;
}
/* set method */
static AST  setTagInfo( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astName = value;
}
//	IDX_SYMDEFINFO
static AST  getNext( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astNext;
}
/* set method */
static AST  setNext( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		assert_index( maxTable, index, value );
		return 0;
	}
	return pTbl->astNext = value;
}

static AST getLeft( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild;
}
static AST setLeft( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild = value;
}
static AST getRight( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild;
}
static AST setRight( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild = value;
}
static AST getParent( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent;
}
static AST setParent( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent = value;
}
static AST getIndex( AST index )
{
	return index;
}
static U2 getColor( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor;
}
static U2 setColor( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor = value;
}
static ACCESS_RBTREE stAccessRbtree =
{
		AllocTable,
		getMaxLineNum,
		getIndex,
		{
				getLeft,
				setLeft
		},
		{
				getRight,
				setRight
		},
		{
				getParent,
				setParent
		},
		{
				getColor,
				setColor
		}
};
static AST  findTypeInfo(AST root, VOID* pdata, S4 (*comp)( AST, VOID* )){
	S4 nCmpResult = 0;
	AST astParent = 0;
	if( root > 0 ){
		nCmpResult = FindData( &stAccessRbtree, root , pdata ,&astParent, comp );
		if( nCmpResult == 0 ){
			return astParent;
		}
	}
	return 0;
}
static AST  addTypeInfo(AST root, AST *pNewRoot, VOID* pdata, S4 (*comp)( AST, VOID* )){
	S4 nCmpResult = 0;
	AST astParent = 0;
	AST astNew = 0;
	nCmpResult = FindData( &stAccessRbtree, root , pdata ,&astParent, comp );
	if( nCmpResult == 0 ){
		return astParent;
	}else{
		InsertData( &stAccessRbtree, nCmpResult, astParent, pNewRoot, &astNew );
	}
	return astNew;
}
const TYPEINFO_ACCESS stTypeInfoAccess = {
//		BOOL (*clear)( VOID );
		ClearTable,
//		BOOL (*reset)( VOID );
		ResetTable,
//		BOOL (*alloc)( AST );
		AllocTable,
//		BOOL (*copy)( AST, AST, AST );
		copyTable,
//		AST  (*getMax)( VOID );
		getMaxLineNum,

		{
				getTypeKind,
				setTypeKind
		},
		{
				getSignFlag,
				setSignFlag
		},
		{
				getLongShortFlg,
				setLongShortFlg
		},
		{
				getIntFloatFlag,
				setIntFloatFlag
		},
		{
				getTypeQual,
				setTypeQual
		},
		{
				getSize,
				setSize
		},
		{
				getElementType,
				setElementType
		},
		{
				getNoQualType,
				setNoQualType
		},
		{
				getMemChain,
				setMemChain
		},
		{
				getTagInfo,
				setTagInfo
		},
		{
				getNext,
				setNext
		},
		{
				isUndefined,
				setUndefined
		},
		{
				isVariableSize,
				setVariableSize
		},
		findTypeInfo,
		addTypeInfo
};
/* end of file */

